home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Ken Long / FontShow-1.1.2-c / Think / Fontshow.c next >
Encoding:
Text File  |  1994-12-04  |  21.2 KB  |  834 lines  |  [TEXT/KAHL]

  1. //•-----------------------------------------------------------------------o//
  2. //•    Note from Kenneth A. Long:
  3. //•     I found this source on the net, but it was only the .c and .h file.
  4. //• Then after a time, I found the built application in a different place
  5. //• and was able to get the resources out of it.  Then I made the .r file
  6. //• from it, for general principles.
  7. //• The two includes I excluded caused link errors I couldn't fix, but it
  8. //• seems to run okay at first glance.  I've done hardly any testing, and
  9. //• I'm sure there could be improvements, refinements and more thorough
  10. //• code added.  But HEY!  It's C source!
  11.  
  12. //•      6. March      1994 (v1.1.2) Compiles/runs - Think C v5.0.4/6.0.1
  13. //•-----------------------------------------------------------------------o//
  14. //o The FontShow
  15.  
  16. //o (c) 1989 Rainer Fuchs, Am Weingarten 13, 6000 Frankfurt 90, FRG
  17.  
  18. //o History:
  19. //•     23. November  1994 (v1.1.2) Runs on Code Warrior C.
  20. //•     06. March      1994 (v1.1.2) Runs on Think C.
  21. //o     17. Oktober      1989 (v1.1.1) Compatible with EZ-Menu 
  22. //o     10. Oktober      1989 (v1.1)    Font number added; Fontsize added
  23. //o      3. Oktober      1989 (v1.0)    multiple sample strings
  24. //o      2. Oktober      1989 (v1.0b3) changed print format 
  25. //o     26. September 1989 (v1.0b2) changed print format
  26. //o     25. September 1989 (v1.0b1) added character table 
  27. //o     24. September 1989 (v0.0d1) first hack
  28. //o-----------------------------------------------------------------------o//
  29.  
  30. #include "fontshow.h"
  31.  
  32. //o-----------------------------------------------------------------------o//
  33. //o Error and information handling ---------------------------------------o//
  34.  
  35. void ErrorMsg (int index)
  36. {
  37.     Str255 theString;
  38.  
  39.     InitCursor ();
  40.     GetIndString (theString, 128, index);//o get error msg from rsrc.
  41.     ParamText (theString, "\p", "\p", "\p");
  42.     StopAlert (128, NIL);            //o show error dialog.
  43. }
  44.  
  45. //o-----------------------------------------------------------------------o//
  46.  
  47. void ShowInfo ()                    //o About FontShow….
  48. {
  49.     DialogPtr     myDialog;
  50.     EventRecord    myEvent;
  51.  
  52.     myDialog=GetNewDialog (ABOUT, NIL, (WindowPtr)-1);//o get dialog from rsrc.
  53.     DrawDialog (myDialog);                    //o draw it.
  54.     while (!GetNextEvent (keyDownMask|autoKeyMask|mDownMask, &myEvent))
  55.         SystemTask ();                //o wait until event.
  56.     DisposDialog (myDialog);        //o dispose of dialog.
  57. }
  58.  
  59. //o-----------------------------------------------------------------------o//
  60. //o Sample text output ---------------------------------------------------o//
  61. //o-----------------------------------------------------------------------o//
  62.  
  63. Boolean BreakCheck ()                //o check for break after each page.
  64. {
  65.     EventRecord theEvent;
  66.     WindowPtr     whichWindow;
  67.  
  68.      for (;;) 
  69.      {    //o mouse clicks in menu bar ignored for comp. with EZ-Menu 
  70.          //o *sigh*.
  71.          while (!GetNextEvent (keyDownMask|autoKeyMask|mDownMask, &theEvent))
  72.             SystemTask ();                //o wait until event.
  73.          if ((theEvent.what == keyDown) || (theEvent.what == autoKey)) //o keyDown?.
  74.             return ((char)BitAnd (theEvent.message, charCodeMask) == '.' &&
  75.                         BitAnd (theEvent.modifiers, cmdKey) != 0);
  76.             //o if Cmd-dot    then TRUE else FALSE.
  77.          else
  78.              if (theEvent.what == mouseDown && FindWindow (theEvent.where, &whichWindow)!=inMenuBar)
  79.                      return (FALSE);//o click not in MenuBar.
  80.      }
  81. }
  82.  
  83. //o-----------------------------------------------------------------------o//
  84.  
  85. void DrawPage (int i)//o draw sample page.
  86. {
  87.     myFontInfo     *myFont;
  88.     int             lineNr=0;
  89.     char             *pos;
  90.     register int from, to, nr;
  91.     register int c, x, y;
  92.     int            lineH;
  93.     int            wMax;
  94.     Str255        myStr;
  95.     char            buf[4];
  96.  
  97.     myFont= *fontHdl;                //o Block is already locked !.
  98.     lineH=myFont[i].lineHeight;
  99.     wMax=myFont[i].widMax;
  100.  
  101.     TextFont (0);                    //o draw font name in system font.
  102.     TextSize (12);                    //o and 12 pt size.
  103.     MoveTo (TOPH, TOPF);            //o    goto title line.
  104.     DrawString (myFont[i].fontName);//o draw font name.
  105.     DrawString ("\p (ID=");
  106. //    stci_d (buf, myFont[i].fontNum, 3);    //o KAL toox it out - couldn't...
  107.     DrawString (CtoPstr (buf));
  108.     DrawString ("\p) ");
  109. //    stci_d (buf, fontSize, 2);            //o find in headers.
  110.     DrawString (CtoPstr (buf));
  111.     DrawString ("\p point.");
  112.  
  113.     TextFont (myFont[i].fontNum);//o set font.
  114.     TextSize (fontSize);
  115.  
  116.     if (RealFont (myFont[i].fontNum, fontSize)) 
  117.     {
  118.         //o sample text.
  119.         if (opt == SAMPLE || prFlag) 
  120.         {
  121.             lineNr++;
  122.             HLock (myText);
  123.             from=nr=to=0;
  124.             pos=*myText;
  125.             
  126.             //o break text into single lines.
  127.             while (to<textLength) 
  128.             {    
  129.                 to++;
  130.                 
  131.                 //o search for CR.
  132.                 if ((*pos++ == 0x0D) || (to == textLength)) 
  133.                 {
  134.                     MoveTo (TOPH, TOPV + lineH*lineNr++ );//o go to next line.
  135.                     DrawText (*myText, from, to-from);//o draw line.
  136.                     from = to;
  137.                 }
  138.             }
  139.             HUnlock (myText);
  140.         }
  141.  
  142.         //o sample string.
  143.         if (opt == STRING || prFlag ) 
  144.         {
  145.             for (i=1;i;i++) 
  146.             {
  147.                 lineNr++;
  148.                 GetIndString (myStr, STRINGS, i);
  149.                 if (*myStr) 
  150.                 {
  151.                     MoveTo (TOPH, TOPV + lineH*lineNr++ );
  152.                     DrawString (myStr);
  153.                 }
  154.                 else break;
  155.             }
  156.         }
  157.         //o character map.
  158.         if (opt == MAP || prFlag) 
  159.         {
  160.             lineNr++;
  161.             
  162.             //o only printable characters.
  163.             for (c = 32; c < 256; c++) 
  164.             {    
  165.                 //o 32 char per row.
  166.                 if (c % 32 == 0) 
  167.                 {
  168.                     x = TOPH;
  169.                     y = TOPV + lineH*lineNr++;    //o new line.
  170.                 }
  171.                 MoveTo (x, y);
  172.                 if (isprint (c))                //o skip 127.
  173.                     DrawChar ((char)c);
  174.                 else
  175.                     DrawChar (32);
  176.                 x += wMax;                    //o makes a pretty output.
  177.             }
  178.         }
  179.     }
  180.     else 
  181.         {
  182.             TextSize (12);
  183.             TextFont (0);
  184.             DrawString ("\p Font size not available.");
  185.     }
  186. }
  187.  
  188. //o-----------------------------------------------------------------------o//
  189.  
  190. void ShowFonts ()                    //o Display fonts.
  191. {
  192.     register int i;
  193.     DialogPtr    myDialog;
  194.     WindowPtr     myWindow;
  195.  
  196.     prFlag = FALSE;
  197.  
  198.     if (SelectFonts ()) 
  199.     {            //o only when select dialog is ok.
  200.         myWindow = GetNewWindow (FONTWINDOW, NIL, (WindowPtr)-1L); //o open a window.
  201.         myDialog = GetNewDialog (SCREENBREAK, NIL, myWindow);//o show break information.
  202.         DrawDialog (myDialog);
  203.         SetPort (myWindow);
  204.         HLock (fontHdl);
  205.         for (i = 0; i < fontCount; i++) 
  206.         {
  207.             //o draw only selected fonts.
  208.             if ((*fontHdl)[i].flag)
  209.             {
  210.                 EraseRect (&myWindow->portRect);    //o    clear page.
  211.                 DrawPage (i);                        //o draw sample page.
  212.                 if (BreakCheck ())                    //o check for break.
  213.                     break;
  214.             }
  215.         }
  216.         HUnlock (fontHdl);
  217.         DisposeWindow (myWindow);//o close window and dialog.
  218.         DisposDialog (myDialog);
  219.     }
  220. }
  221.  
  222. //o-----------------------------------------------------------------------o//
  223.  
  224. int HowMany ()
  225. {
  226.     return (((**prRecHdl).prJob.bJDocLoop == bDraftLoop) ? 
  227.                 (**prRecHdl).prJob.iCopies : 1 );
  228. }
  229.  
  230. //o-----------------------------------------------------------------------o//
  231.  
  232. void PrintFonts ()                    //o print fonts.
  233. {
  234.     TPPrPort        myPrPort;
  235.     TPrStatus    myPrStatus;
  236.     DialogPtr     mySpoolDlg, myPrintDlg;
  237.     GrafPtr        savePort;
  238.     register int i;
  239.     register int copies;
  240.     int             curs=0;
  241.  
  242.     prFlag=TRUE;                    //o indicate printing.
  243.  
  244.     GetPort (&savePort);            //o save old grafPort.
  245.     PrOpen ();                        //o open PrintMgr.
  246.     
  247.     //o get print job infos.
  248.     if (SelectFonts () && PrJobDialog (prRecHdl)) 
  249.     { 
  250.         //o just in case last printout was cancelled by user:.
  251.         PrintErr=noErr;
  252.         for (copies = HowMany (); copies > 0 && PrintErr == noErr; copies--) 
  253.         {
  254.             //o show status information:.
  255.             if ((**prRecHdl).prJob.bJDocLoop == bSpoolLoop) 
  256.                 DrawDialog (mySpoolDlg=GetNewDialog (SPOOLINFO, NIL, (WindowPtr)-1));
  257.             else
  258.                 DrawDialog (myPrintDlg=GetNewDialog (PRINTINFO, NIL, (WindowPtr)-1));
  259.  
  260.             SetCursor (*myCursor[curs++]);//o we´re busy (rotating watch.
  261.             if (curs == 4) curs = 0;
  262.  
  263.             myPrPort = PrOpenDoc (prRecHdl, NIL, NIL); //o open printPort.
  264.             SetPort (myPrPort);
  265.             TextSize (12);
  266.             HLock (fontHdl);
  267.             for (i = 0; i < fontCount; i++) 
  268.             {
  269.  
  270.                 SetCursor (*myCursor[curs++]);//o still busy.
  271.                 if (curs == 4) curs = 0;
  272.  
  273.                 if (PrintErr == noErr) 
  274.                 {
  275.                     //o print only selected fonts.
  276.                     if ((*fontHdl)[i].flag)
  277.                     {
  278.                         PrOpenPage (myPrPort, NIL);
  279.                         if (PrintErr == noErr)
  280.                             DrawPage (i);            //o print sample page.
  281.                         PrClosePage (myPrPort);
  282.                     }
  283.                 }
  284.             }
  285.             HUnlock (fontHdl);
  286.             PrCloseDoc (myPrPort);//o close printPort.
  287.  
  288.             //o now print spool file.
  289.             if ((**prRecHdl).prJob.bJDocLoop == bSpoolLoop && PrintErr == noErr) 
  290.             {
  291.                 DisposDialog (mySpoolDlg);
  292.                 DrawDialog (myPrintDlg=GetNewDialog (PRINTINFO, NIL, (WindowPtr)-1));
  293.                 PrPicFile (prRecHdl, NIL, NIL, NIL, &myPrStatus); //o print spool file.
  294.             }
  295.  
  296.             DisposDialog (myPrintDlg);
  297.         }
  298.  
  299.         InitCursor ();
  300.         if (PrintErr!=noErr)
  301.                 ErrorMsg (ERR_PRINT);//o something went wrong.
  302.         else
  303.             SysBeep (10);            //o notify user.
  304.         PrClose ();                    //o close printing manager.
  305.         SetPort (savePort);        //o reset old grafPort.
  306.     }
  307. }
  308.  
  309. //o-----------------------------------------------------------------------o//
  310. //o Font dialog handling -------------------------------------------------o//
  311. //o-----------------------------------------------------------------------o//
  312.  
  313. pascal void    DrawList(WindowPtr theWindow, int itemNo)
  314. {
  315.     Rect         rView;
  316.     RgnHandle theRgn;
  317.     int         itemType;
  318.     Handle     item;
  319.     Rect         box;
  320.  
  321.     //o get rect of OK button.
  322.     GetDItem (theWindow, OK, &itemType, &item, &box); 
  323.     PenSize (3, 3);
  324.     InsetRect (&box, -4, -4);
  325.     FrameRoundRect (&box, 16, 16);//o draw bold.
  326.     PenSize (1, 1);
  327.  
  328.     HLock (myList);
  329.     rView= (**myList).rView;
  330.     InsetRect (&rView, -1, -1);
  331.     FrameRect (&rView);            //o frame around list.
  332.     theRgn= (*theWindow).visRgn;
  333.     LUpdate (theRgn, myList);        //o update list.
  334.     HUnlock (myList);
  335. }
  336.  
  337. //o-----------------------------------------------------------------------o//
  338.  
  339. pascal Boolean    myFilter(DialogPtr theDialog, 
  340.                             EventRecord *theEvent, 
  341.                                 int *itemHit)
  342. {
  343.     char     charCode;
  344.     GrafPtr savePort;
  345.     Cell     theCell;
  346.     Boolean flag;
  347.  
  348.     if ((theEvent->what == keyDown) || (theEvent->what == autoKey)) 
  349.     { 
  350.         charCode= (char)BitAnd (theEvent->message, charCodeMask);
  351.         
  352.         //o OK button is default.
  353.         if ((charCode == 13) || (charCode == 3)) 
  354.         { 
  355.             *itemHit=1;
  356.             return (TRUE);
  357.         }
  358.         else if ((charCode == '.') && (BitAnd (theEvent->modifiers, cmdKey) != 0)) 
  359.         {                                //o Cmd-dot cancels dialog.
  360.             *itemHit=2;
  361.             return (TRUE);
  362.         }
  363.         else return (FALSE);
  364.     }
  365.  
  366.     else 
  367.         {
  368.             if (*itemHit == FLIST) 
  369.             {    //o list handling.
  370.                     GetPort (&savePort);
  371.                     SetPort ((**myList).port);
  372.  
  373.                     //o convert to local coordinates.
  374.                     GlobalToLocal (& (theEvent->where));
  375.  
  376.                     //o simulate cmd key to fool default list
  377.                     //o definition routine !!!.
  378.                     flag = LClick (theEvent->where, 
  379.                                    theEvent->modifiers|cmdKey, 
  380.                                    myList);
  381.                                    
  382.                     SetPort (savePort);
  383.             }
  384.  
  385.             if (flag == TRUE) 
  386.             {        //o double click is equal to ok.
  387.                     *itemHit=1;
  388.                     return (TRUE);
  389.                 }
  390.                 else
  391.                     return (FALSE);
  392.     }
  393. }
  394.  
  395. //o-----------------------------------------------------------------------o//
  396. //o Initialize font list. ------------------------------------------------o//
  397. //o-----------------------------------------------------------------------o//
  398.  
  399. void InitList (WindowPtr theWindow)
  400. {
  401.     int             itemType;
  402.     Handle         item;
  403.     Point         csize, theCell;
  404.     Rect             rView, dataBounds;
  405.     register int i;
  406.     myFontInfo     *myFont;
  407.  
  408.     GetDItem (theWindow, FLIST, &itemType, &item, &rView);
  409.     SetRect (&dataBounds, 0, 0, 1, fontCount);//o set borders of list.
  410.     SetPt (&csize, 0, 0);
  411.     rView.right-=15;                //o leave room for scroll bar.
  412.     myList=LNew (&rView, &dataBounds, csize, 0, theWindow, TRUE, FALSE, FALSE, TRUE);
  413.  
  414.     HLock (fontHdl);
  415.     myFont= *fontHdl;
  416.  
  417.     //o fill list with entries from myFontInfo array.
  418.     for (i=0; i<fontCount;i++) 
  419.     {
  420.         SetPt (&theCell, 0, i);
  421.         LSetCell (myFont[i].fontName+1, *myFont[i].fontName, theCell, myList);
  422.         LSetSelect (myFont[i].flag, theCell, myList);//o preselect entries.
  423.     }
  424.     HUnlock (fontHdl);
  425. }
  426.  
  427. //o-----------------------------------------------------------------------o//
  428.  
  429. Boolean SelectFonts ()            //o handle font select dialog.
  430.     register int i;
  431.     int            button;
  432.     int             itemType;
  433.     Handle         item;
  434.     Rect             box;
  435.     Cell             theCell;
  436.     Boolean         stop=FALSE;
  437.     myFontInfo     *myFont;
  438.     DialogPtr     myDialog=GetNewDialog (FONTLIST, NIL, (WindowPtr)-1);
  439.                                             //o Auswahl-Dialog.
  440.  
  441.     GetDItem (myDialog, FLIST, &itemType, &item, &box);//o get rect of list button.
  442.     SetDItem (myDialog, FLIST, userItem, DrawList, &box); //o install user item.
  443.  
  444.     InitList (myDialog);             //o initialize list.
  445.  
  446.     ShowWindow (myDialog);        //o make dialog visible.
  447.     while (!stop) 
  448.     {
  449.         SystemTask ();
  450.         ModalDialog (myFilter, &button);//o Dialog anzeigen.
  451.  
  452.         switch (button) 
  453.         {
  454.             case EXALL:                //o deselect all fonts.
  455.                 for (i=0; i<fontCount;i++) 
  456.                 {
  457.                         SetPt (&theCell, 0, i);
  458.                         LSetSelect (FALSE, theCell, myList);
  459.                 }
  460.             break;
  461.  
  462.             case INALL:                //o select all fonts.
  463.                 for (i=0; i<fontCount;i++) 
  464.                 {
  465.                         SetPt (&theCell, 0, i);
  466.                         LSetSelect (TRUE, theCell, myList);
  467.                 }
  468.             break;
  469.  
  470.             case REVERT:            //o revert to old. 
  471.                 HLock (fontHdl);
  472.                 myFont= *fontHdl;
  473.                 for (i=0; i<fontCount;i++) 
  474.                 {
  475.                         SetPt (&theCell, 0, i);
  476.                         LSetSelect (myFont[i].flag, theCell, myList);
  477.                 }
  478.                 HUnlock (fontHdl);
  479.             break;
  480.  
  481.             case Cancel:            //o dialog cancelled.
  482.                 stop=TRUE;
  483.             break;
  484.  
  485.             case OK:
  486.                  HLock (fontHdl);
  487.                 myFont= *fontHdl;
  488.                 for (i=0; i<fontCount;i++) 
  489.                 {//o update myFontInfo array.
  490.                         SetPt (&theCell, 0, i);
  491.                         myFont[i].flag=LGetSelect (FALSE, &theCell, myList);
  492.                 }
  493.                 HUnlock (fontHdl);
  494.                 stop=TRUE;
  495.                 break;
  496.         } //o end case.
  497.     }
  498.  
  499.     LDispose (myList);                //o dispose of list.
  500.     DisposDialog (myDialog);                    //o dispose of dialog.
  501.     return ((button == OK)?TRUE:FALSE);
  502. }
  503.  
  504. //o-----------------------------------------------------------------------o//
  505. //o Event handling -------------------------------------------------------o//
  506. //o-----------------------------------------------------------------------o//
  507.  
  508. void HandleMenu (sel)                //o handle menu bar.
  509. long sel;
  510. {
  511.     int     theItem=LoWord (sel);
  512.     Str255 name;
  513.     GrafPtr savePort;
  514.     register int i;
  515.     myFontInfo *myFont;
  516.     FontInfo    fInfo;
  517.  
  518.     switch (HiWord (sel)) 
  519.     {
  520.         case APPLE_M:
  521.             GetItem (myMenus[APPLE], theItem, &name);
  522.             if (theItem == INFO)        //o About….
  523.                 ShowInfo ();
  524.             else 
  525.                 {
  526.                     GetPort (&savePort);//o save old grafPort (just in case…).
  527.                     OpenDeskAcc (&name);//o open DA.
  528.                     InitCursor ();        //o DA may have changed cursor.
  529.                     SetPort (savePort);    //o    DA may have changed grafPort.
  530.             }
  531.         break;
  532.  
  533.         case FILE_M:
  534.             switch (theItem) 
  535.             {
  536.                 case SETUP:
  537.                     PrOpen ();        //o open/close PrintMgr acc. to TN.
  538.                     PrStlDialog (prRecHdl);//o get page setup.
  539.                     PrClose ();
  540.                 break;
  541.  
  542.                 case DISPLAY:
  543.                     ShowFonts ();
  544.                 break;
  545.  
  546.                 case PRINT:
  547.                     PrintFonts ();
  548.                 break;
  549.  
  550.                 case QUIT:
  551.                     quit=TRUE;
  552.                 break;
  553.             }
  554.         break;
  555.  
  556.         case EDIT_M: 
  557.             SystemEdit (theItem-1); //o our application doesn´t handle EDIT menu.
  558.         break;
  559.  
  560.         case OPTIONS_M:
  561.             CheckItem (myMenus[OPTIONS], opt, FALSE); //o deselect menu item.
  562.             CheckItem (myMenus[OPTIONS], opt=theItem, TRUE); //o select new Item.
  563.         break;
  564.  
  565.         case SIZE_M:
  566.             if (theItem!=oldItem) 
  567.             {
  568.                 SetCursor (*myCursor[0]);
  569.                 CheckItem (myMenus[SIZE], oldItem, FALSE);
  570.                 CheckItem (myMenus[SIZE], oldItem=theItem, TRUE);
  571.                 switch (oldItem) 
  572.                 {
  573.                     case SIZE9:
  574.                         fontSize=9;
  575.                     break;
  576.  
  577.                     case SIZE10:
  578.                         fontSize=10;
  579.                     break;
  580.  
  581.                     case SIZE12:
  582.                         fontSize=12;
  583.                     break;
  584.  
  585.                     case SIZE14:
  586.                         fontSize=14;
  587.                     break;
  588.  
  589.                     case SIZE18:
  590.                         fontSize=18;
  591.                     break;
  592.  
  593.                     case SIZE20:
  594.                         fontSize=20;
  595.                     break;
  596.  
  597.                     case SIZE24:
  598.                         fontSize=24;
  599.                     break;
  600.                 }
  601.                 TextSize (fontSize);
  602.                 HLock (fontHdl);
  603.                 myFont= *fontHdl;
  604.                 for (i = 0; i < fontCount; i++) 
  605.                 {
  606.                 TextFont (myFont[i].fontNum);                //o set font.
  607.                 GetFontInfo (&fInfo);                            //o and get font info.
  608.                 myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; //o calc. line height.
  609.                 myFont[i].widMax=fInfo.widMax;            //o calc. char width.
  610.             }
  611.             HUnlock (fontHdl);
  612.             TextSize (12);
  613.             TextFont (0);
  614.             InitCursor ();
  615.         }
  616.         break;
  617.     }
  618.     HiliteMenu (0);
  619. }
  620.  
  621. //o-----------------------------------------------------------------------o//
  622.  
  623. void HandleMouseDown (theEvent)//o handle mouseDown events.
  624. EventRecord theEvent;
  625. {
  626.     WindowPtr whichWindow;
  627.  
  628.     switch (FindWindow (theEvent.where, &whichWindow )) 
  629.     {
  630.         case inDesk:
  631.             SysBeep (10);
  632.         break;
  633.  
  634.         case inMenuBar:
  635.             HandleMenu (MenuSelect (theEvent.where) ) ;
  636.         break;
  637.  
  638.         case inSysWindow:
  639.             SystemClick (&theEvent, whichWindow );
  640.         break;
  641.     } //o end switch.
  642. }
  643.  
  644. //o-----------------------------------------------------------------------o//
  645. //o MainLoop () -----------------------------------------------------------o//
  646. //o-----------------------------------------------------------------------o//
  647.  
  648. void MainLoop ()
  649. {
  650.     EventRecord theEvent;
  651.  
  652.     quit = FALSE;
  653.     while (!quit)
  654.     {
  655.         SystemTask ();
  656.         if (GetNextEvent (everyEvent, &theEvent))
  657.             switch (theEvent.what) 
  658.             {
  659.                 case mouseDown:    //o mouse click.
  660.                     HandleMouseDown (theEvent);
  661.                 break;
  662.  
  663.                 case keyDown:         //o key click.
  664.                 case autoKey:
  665.                     if ((theEvent.modifiers & cmdKey) != 0) //o we handle only Cmd-key.
  666.                         HandleMenu (MenuKey ((char) (theEvent.message & charCodeMask)));
  667.                 break;
  668.             } //o end switch (and if).
  669.     }//o end while.
  670. }
  671.  
  672. //o-----------------------------------------------------------------------o//
  673. //o Initialization and main () --------------------------------------------o//
  674. //o-----------------------------------------------------------------------o//
  675.  
  676. void InitMenu ()                    //o Draw menu bar.
  677. {
  678.     register int i;
  679.  
  680.     myMenus[APPLE]=GetMenu (APPLE_M);
  681.     AddResMenu (myMenus[APPLE], 'DRVR');//o insert DAs in Apple-menu.
  682.  
  683.     myMenus[FILE]=GetMenu (FILE_M);
  684.     myMenus[EDIT]=GetMenu (EDIT_M);
  685.     myMenus[OPTIONS]=GetMenu (OPTIONS_M);
  686.     myMenus[SIZE]=GetMenu (SIZE_M);
  687.  
  688.     CheckItem (myMenus[OPTIONS], opt = SAMPLE, TRUE); //o preselect "sample text".
  689.     CheckItem (myMenus[SIZE], oldItem=SIZE12, TRUE); //o preselect font size 12.
  690.     fontSize=12;
  691.  
  692.     for (i = 0; i < 5; InsertMenu (myMenus[i++], 0))
  693.         ;
  694.     DrawMenuBar ();
  695. }
  696.  
  697. //o-----------------------------------------------------------------------o//
  698.  
  699. void InitText ()                    //o get sample text from rsrc.
  700. {
  701.     myText = GetResource ('TEXT', 128);
  702.     textLength= (int)SizeResource (myText);
  703. }
  704.  
  705. //o-----------------------------------------------------------------------o//
  706.  
  707. void InitPrint ()
  708. {
  709.     PrOpen ();
  710.     prRecHdl= (THPrint)NewHandle (sizeof (TPrint));//o get new print record.
  711.     if (prRecHdl)
  712.         PrintDefault (prRecHdl);    //o validate it.
  713.     else
  714.         ErrorMsg (ERR_NOROOM);
  715.     PrClose ();
  716. }
  717.  
  718. //o-----------------------------------------------------------------------o//
  719.  
  720. void BuildList ()                    //o build font list.
  721. {
  722.     register     int i;
  723.     myFontInfo    *myFont;
  724.     FontInfo     fInfo;
  725.     MenuHandle    tempMenu;
  726.  
  727.     //o Get a fake menu; use the Menu Manager to fill it with font names - 
  728.     //o the Menu Manager sorts entries alphabetically !.
  729.  
  730.     tempMenu = NewMenu (TEMP_M, "x");
  731.     AddResMenu (tempMenu, 'FONT');
  732.     fontCount=CountMItems (tempMenu);
  733.  
  734.     //o dynamic allocation of heap space for myFontInfo array.
  735.     if (fontHdl= (myFontInfo **)NewHandle (fontCount*sizeof (myFontInfo))) 
  736.     {
  737.         HLock (fontHdl);
  738.         TextSize (12);
  739.         myFont= *fontHdl;
  740.         for (i=0;i<fontCount;i++) 
  741.         {
  742.                 GetItem (tempMenu, i+1, myFont[i].fontName);//o get font names.
  743.             GetFNum (myFont[i].fontName, &myFont[i].fontNum);//o get font numbers.
  744.             TextFont (myFont[i].fontNum);                //o set font.
  745.             GetFontInfo (&fInfo);                            //o and get font info.
  746.             myFont[i].lineHeight = fInfo.ascent+fInfo.descent+fInfo.leading; //o calc. line height.
  747.             myFont[i].widMax=fInfo.widMax;            //o calc. char width.
  748.             myFont[i].flag=TRUE;                            //o preselect font.
  749.         }
  750.         HUnlock (fontHdl);
  751.     }
  752.     else
  753.         ErrorMsg (ERR_NOROOM);
  754.  
  755.     DisposeMenu (tempMenu);//o We’re done with the menu; dispose of it.
  756.     TextFont (0);    //o and reset system font.
  757. }
  758.  
  759. //o-----------------------------------------------------------------------o//
  760.  
  761. void InitThings ()
  762. {
  763.     DialogPtr myDialog;
  764.     register int i;
  765.  
  766.     InitGraf (&thePort); 
  767.     MaxApplZone ();                        //o we want it all, and we want it NOW !.
  768.     MoreMasters ();                    //o extra pointer blocks at the bottom of the heap.
  769.     MoreMasters ();                    //o this is 5 X 64 master pointers.
  770.     MoreMasters ();
  771.     MoreMasters ();
  772.     MoreMasters ();
  773.  
  774.     InitFonts ();            //o startup the font manager.
  775.     InitWindows ();                        //o startup the window manager.
  776.     InitMenus ();                    //o startup the menu manager.
  777.     TEInit ();                        //o startup the text edit manager.
  778.     InitDialogs (NIL);                //o startup the dialog manager.
  779.  
  780.     FlushEvents (everyEvent, 0);
  781.  
  782. //o get the cursors we use and lock them down - no clutter.
  783.  
  784.     myCursor[0] = GetCursor (watchCursor);
  785.     myCursor[1] = GetCursor (128);
  786.     myCursor[2] = GetCursor (129);
  787.     myCursor[3] = GetCursor (130);
  788.  
  789.     for (i = 0; i < 4; i++) 
  790.     {
  791.         MoveHHi ((Handle)myCursor[i]);
  792.         HLock ((Handle)myCursor[i]);
  793.     }
  794.  
  795.     //o tell user we´re busy.
  796.     myDialog = GetNewDialog (WAIT, NIL, (WindowPtr)-1); 
  797.     DrawDialog (myDialog);
  798.     InitMenu ();                        //o build menus.
  799.     InitText ();                        //o get sample text.
  800.     InitPrint ();                    //o initialize printing.
  801.     BuildList ();                    //o build font name list.
  802.     DisposDialog (myDialog);
  803.     InitCursor ();                    //o show arrow cursor.
  804. }
  805.  
  806. //o-----------------------------------------------------------------------o//
  807.  
  808. void CleanUp ()
  809. {
  810.     register int i;
  811.  
  812.     if (fontHdl)
  813.         DisposHandle (fontHdl);
  814.     if (prRecHdl)
  815.         DisposHandle (prRecHdl);
  816.     for (i=0;i<4;i++)
  817.         HUnlock ((Handle)myCursor[i]);
  818. }
  819.  
  820. //o-----------------------------------------------------------------------o//
  821.  
  822. void main ()
  823. {
  824.     InitThings ();                            //o initializations.
  825.     if (fontHdl && prRecHdl)
  826.         MainLoop ();                            //o main routine.
  827.     CleanUp ();                                //o clean up.
  828. }
  829.  
  830. //o-----------------------------------------------------------------------o//
  831. //o Show's over, folks!
  832. //o-----------------------------------------------------------------------o//
  833.